class Label < ActiveRecord::Base
  validates_presence_of :name
  validates_uniqueness_of :name
  has_one :image, :as => :owner
  
  after_save :set_label_to_image
  
  def uploaded_image
    image || self.build_image
  end
  
  def uploaded_data=(data)
    uploaded_image.uploaded_data = data
  end
  
  def uploaded_data
    uploaded_image.uploaded_data
  end
  
  class << self
    
    def find_xerox
      find( :first, :conditions => [ 'name like ?', 'Xerox' ]  )
    end
    
    def find_hp
      find( :first, :conditions => [ 'name like ?', 'HP' ]  )
    end
    
  end
  
  private
  
  def set_label_to_image
    if image
      image.owner = self
      image.save!
    end
  end  
  
end
